<?php
/**
 * Created by PhpStorm.
 * User: asus
 * Date: 2017/2/26
 * Time: 15:57
 */

namespace Home\Controller;


use Home\Model\TypeModel;
use Org\Util\DataTree;

class ListController extends CommonController
{
    public function index()
    {
        $tree = new DataTree();
        $data = $tree->tree((new TypeModel())->select(),'typename','cid');
        foreach ($data as $k => $v){
            if($data[$k]['type_is_obj']==0){
                $data[$k]['type_is_obj']='否';
            }else{
                $data[$k]['type_is_obj']='是';
            }
            if($data[$k]['type_value']==0){
                $data[$k]['type_value']='不是收费项';
            }else{
                $data[$k]['type_value']='￥：'.$data[$k]['type_value'].'  rmb';
            }
        }
        $this->assign('data',$data);
        $this->display();
    }

    public function store()
    {
        if(IS_POST){
            $data = i('post.');
            if(trim(empty($data['typename']))){
                $this->error('分类名不能为空！');
            }
            $date = [
                'typename'=>$data['typename'],
                'type_time'=>time(),
                'type_is_obj'=>$data['is_obj'],
                'type_value'=>$data['value'],
                'pid'=>$data['pid'],
            ];
            $res = (new TypeModel())->add($date);
            if($res){
                $this->success('操作成功',u('List/index'));
            }else{
                $this->error('操作失败');
            }
        }else {
            $this->display();
        }
    }

    public function addson()
    {
        if(IS_POST){
            $data = i('post.');
            if(trim(empty($data['typename']))){
                $this->error('分类名不能为空！');
            }
            $date = [
                'typename'=>$data['typename'],
                'type_time'=>time(),
                'type_is_obj'=>$data['is_obj'],
                'type_value'=>$data['value'],
                'pid'=>$data['pid'],
            ];
            $res = (new TypeModel())->add($date);
            if($res){
                $this->success('操作成功',u('List/index'));
            }else{
                $this->error('操作失败');
            }
        }else {
            $id = i('get.id');
            $typeid = (new TypeModel())->where(['cid'=>$id])->select();
            $this->assign('typeid',$typeid);
            $this->display();
        }
    }
    public function edit()
    {
        $id = i('get.id');
        if(IS_POST){
            if(trim(empty(i('post.typename')))){
                $this->error('分类名不能为空');
            }
            $data = [
                'typename'=>i('post.typename'),
                'type_value'=>i('post.value'),
            ];
            $res = (new TypeModel())->where(['cid'=>$id])->save($data);
            if($res){
                $this->success('操作成功',u('List/index'));
            }else{
                $this->error('操作失败');
            }
        }else {
            $olddata = (new TypeModel())->where(['cid'=>$id])->find();
            $this->assign('olddata',$olddata);
            $this->display();
        }
    }
    public function delect()
    {
        $id = i('get.id');
        $pid = (new TypeModel())->where(['cid'=>$id])->find();
        $data = ['pid'=>$pid['pid']];
        (new TypeModel())->where(['pid'=>$id])->save($data);
        $res = (new TypeModel())->where(['cid'=>$id])->delete();
        if($res){
            $this->success('操作成功',u('List/index'));
        }else{
            $this->error('操作失败');
        }
    }
}